package com.alibaba.citrus.ots.sales.plan.repository;

import com.alibaba.citrus.ots.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.PageQuerySendHistoryRequest;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.QueryNotificationIDRequest;
import com.alibaba.citrus.ots.sales.plan.sdo.SendHistoryRecordSDO;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcots.model.dto.SendHistoryRecordLoadListRequest;
import com.epoch.app.bcots.model.enums.NoticeTitle;
import com.epoch.app.bcots.sales_plan.sendhistoryrecord.dto.LoadSendHistoryRecordListRequest;
import com.epoch.app.bcots.sales_plan.sendhistoryrecord.model.SendHistoryRecord;
import com.epoch.app.bcots.sales_plan.sendhistoryrecord.service.SendHistoryRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class SendHistoryRecordRepositoryImpl implements SendHistoryRecordRepository {

    @Resource
    private SendHistoryRecordService sendHistoryRecordService;

    /**
     * 分页从查询
     *
     * @param pageQuerySendHistoryRequest
     * @return
     */
    @Override
    @RepositoryInvoker(printLog = true)
    public PageResult<List<SendHistoryRecordSDO>> pageQuery(PageQuerySendHistoryRequest pageQuerySendHistoryRequest) {
        LoadSendHistoryRecordListRequest sendHistoryRecordLoadListRequest = sendHistoryRecordQueryConverter(pageQuerySendHistoryRequest);
        Result<List<SendHistoryRecord>> listResult = sendHistoryRecordService.loadSendHistoryRecordList(sendHistoryRecordLoadListRequest);
        List<SendHistoryRecordSDO> sendHistoryRecordSDOS = listResult.getResult().stream().map(sendHistoryRecord -> {
            SendHistoryRecordSDO sendHistoryRecordSDO = new SendHistoryRecordSDO();
            BeanUtils.copyProperties(sendHistoryRecord, sendHistoryRecordSDO);
            sendHistoryRecordSDO.setLabel(NoticeTitle.of(Integer.valueOf(sendHistoryRecord.getTitle())).getLabel());
            return sendHistoryRecordSDO;
        }).collect(Collectors.toList());
        return PageResult.of(sendHistoryRecordSDOS, listResult.getTotal());
    }


    private LoadSendHistoryRecordListRequest sendHistoryRecordQueryConverter(PageQuerySendHistoryRequest pageQuerySendHistoryRequest) {
        LoadSendHistoryRecordListRequest loadListRequest = new LoadSendHistoryRecordListRequest();
        BeanUtils.copyProperties(pageQuerySendHistoryRequest, loadListRequest);
        return loadListRequest;
    }

    /**
     * 发送邮件新增记录
     *
     * @param sendHistoryRecordSDO
     * @return
     */
    @Override
    public void save(SendHistoryRecordSDO sendHistoryRecordSDO) {
        SendHistoryRecord sendHistoryRecord = new SendHistoryRecord();
        BeanUtils.copyProperties(sendHistoryRecordSDO, sendHistoryRecord);
        sendHistoryRecordService.createSendHistoryRecord(sendHistoryRecord);
    }


    @Override
    public Result<List<SendHistoryRecordSDO>> queryNotification(QueryNotificationIDRequest queryNotificationIDRequest) {
        LoadSendHistoryRecordListRequest request = new LoadSendHistoryRecordListRequest();
        return null;
    }
}
